package com.itheima.dbservice;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class userInfoServiceImpl implements UserInfoServiceApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void saveUserInfo(UserInfo userInfo) {
//        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(UserInfo::getId, userInfo.getId());
        userInfoMapper.saveUserInfo(userInfo);
    }

    @Override
    public void updateImage(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo getUserInfoById(Long jid) {
        return userInfoMapper.getUserInfoById(jid);
    }

    @Override
    public List<UserInfo> getAllUser() {
        return userInfoMapper.selectList(new LambdaQueryWrapper<>());
    }

    @Override
    public UserInfo getUserInfoByIdAndKeyword(Long friendId, String keyword) {
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserInfo::getId, friendId);
        lqw.like(UserInfo::getNickname, keyword);
        return userInfoMapper.selectOne(lqw);
    }
    /**
     * 更新文件路径
     * @param userInfo
     */
    @Override
    public void saveImagePath(UserInfo userInfo) {
        //获取userinfo
        UserInfo userInfoById = userInfoMapper.getUserInfoById(userInfo.getId());
        //将结果复制
        BeanUtil.copyProperties(userInfo,userInfoById);
        userInfoMapper.updateById(userInfoById);
    }

    //模糊查询
    @Override
    public List<UserInfo> selectByIds(List<Long> userIds, String nickname) {

        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        if(CollUtil.isNotEmpty(userIds)){
            wrapper.in(UserInfo::getId,userIds);
        }
        if(StrUtil.isNotEmpty(nickname)){
            wrapper.like(UserInfo::getNickname,nickname);
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(wrapper);
        return userInfos;
    }
}
